package com.web.java0328springboot.controller;


import com.web.java0328springboot.entity.PageVO;
import com.web.java0328springboot.entity.ResponseResult;
import com.web.java0328springboot.entity.Student;
import com.web.java0328springboot.service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


/**
 * 学生控制器
 */
@CrossOrigin
@RestController
public class StudentController {

    @Resource
    private StudentService studentService;

    @RequestMapping(value = "/students-page",method = RequestMethod.GET)
    public ResponseResult findStudentsPage(@RequestParam(value = "current",required = false,defaultValue = "1") Integer current,
                                           @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize,
                                           @RequestParam(value = "name",required = false,defaultValue = "")String name){
        PageVO studentPage = studentService.findStudentPage(current, pageSize, name);
        return new ResponseResult(200,"OK",studentPage);
    }

    @RequestMapping(value = "/student",method = RequestMethod.POST)
    public ResponseResult addStudent(Student student){
        studentService.save(student);
        return new ResponseResult(200,"OK",null);
    }

    @RequestMapping(value = "/student",method = RequestMethod.PUT)
    public ResponseResult updateStudent(Student student){
        studentService.updateById(student);
        return new ResponseResult(200,"OK",null);
    }

    @RequestMapping(value = "/student/{id}",method = RequestMethod.DELETE)
    public ResponseResult deleteStudent(@PathVariable Integer id){
        studentService.removeById(id);
        return new ResponseResult(200,"OK",null);
    }
}
